package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 分页条件查询
     *
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());

        //转换时间的类型
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();

        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));

        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);
        return PageResponse.of(page, DeviceDataVo.class);
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //1.查询指定时间段内的设备数据
        List<DeviceDataGraphVo> list = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        //2.从startTime开始，直到当天结束的所有时间点的DeviceDataGraphVo对象
        List<DeviceDataGraphVo> allList = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
        //3.将查询到的数据列表list转换为一个Map，其中键是时间点（dateTime），值是对应的数据值（dataValue）
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        /*4.遍历allList中的每个DeviceDataGraphVo对象（代表一个预期的时间点）
        尝试从之前创建的map中查找对应时间点的数据值。如果找到了（即value不为null或空），则将该值设置到当前对象的dataValue字段中*/
        allList.stream().map(vo -> {
            Double value = map.get(vo.getDateTime());
            if (ObjectUtil.isNotEmpty(value)) {
                vo.setDataValue(value);
            }
            return vo;
        }).collect(Collectors.toList());

        return allList;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        List<DeviceDataGraphVo> list = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        List<DeviceDataGraphVo> allLIst = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        allLIst.stream().map(vo -> {
            String key = vo.getDateTime();
            Double value = map.get(key);
            if (ObjectUtil.isNotEmpty(value)) {
                vo.setDataValue(value);
            }
            return vo;
        }).collect(Collectors.toList());
        return allLIst;

    }
}
